package com.ruoyi.project.system.newregister.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.newregister.domain.NewRegister;
import com.ruoyi.project.system.newregister.service.INewRegisterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新人注册配置Controller
 * 
 * @author LCL
 * @date 2021-01-20
 */
@Controller
@RequestMapping("/system/newregister")
public class NewRegisterController extends BaseController
{
    private String prefix = "system/newregister";

    @Autowired
    private INewRegisterService newRegisterService;

    @RequiresPermissions("system:newregister:view")
    @GetMapping()
    public String newregister()
    {
        return prefix + "/newregister";
    }

    @RequiresPermissions("system:newregister:view")
    @GetMapping("/home")
    public String shopconfig1(ModelMap map)
    {
        map.put("newRegister",this.newRegisterService.findAll().get(0));
        return prefix + "/newregister";
    }

    /**
     * 查询新人注册配置列表
     */
    @RequiresPermissions("system:newregister:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NewRegister newRegister)
    {
        startPage();
        List<NewRegister> list = newRegisterService.selectNewRegisterList(newRegister);
        return getDataTable(list);
    }

    /**
     * 导出新人注册配置列表
     */
    @RequiresPermissions("system:newregister:export")
    @Log(title = "新人注册配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NewRegister newRegister)
    {
        List<NewRegister> list = newRegisterService.selectNewRegisterList(newRegister);
        ExcelUtil<NewRegister> util = new ExcelUtil<NewRegister>(NewRegister.class);
        return util.exportExcel(list, "newregister");
    }

    /**
     * 新增新人注册配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增保存新人注册配置
     */
    @RequiresPermissions("system:newregister:add")
    @Log(title = "新人注册配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NewRegister newRegister)
    {
        return toAjax(newRegisterService.insertNewRegister(newRegister));
    }

    /**
     * 修改新人注册配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        NewRegister newRegister = newRegisterService.selectNewRegisterById(id);
        mmap.put("newRegister", newRegister);
        return prefix + "/edit";
    }

    /**
     * 修改保存新人注册配置
     */
    @RequiresPermissions("system:newregister:edit")
    @Log(title = "新人注册配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NewRegister newRegister)
    {
        return toAjax(newRegisterService.updateNewRegister(newRegister));
    }

    /**
     * 删除新人注册配置
     */
    @RequiresPermissions("system:newregister:remove")
    @Log(title = "新人注册配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(newRegisterService.deleteNewRegisterByIds(ids));
    }

    @PostMapping("/updatehome")
    @Transactional
    public String updatehomeNewRegister(NewRegister newRegister){
        this.newRegisterService.updateNewRegister(newRegister);
        return "redirect:/"+prefix+"/home";
    }
}
